package N2024.M11.D28;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2025-02-04
 * Time: 21:59
 */
public class test1 {
    static int state=0;
    private static final ReentrantLock lock = new ReentrantLock();
    private static final Condition conditionA = lock.newCondition();
    private static final Condition conditionB = lock.newCondition();
    private static final Condition conditionC = lock.newCondition();

    public static void main(String[] args) throws InterruptedException {
        Thread threadA=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                lock.lock();
                while (state%3!=0){
                    try {
                        conditionA.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("A");
                state++;
                conditionB.signal();
                lock.unlock();
            }
        });

        Thread threadB=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                lock.lock();
                while (state%3!=1){
                    try {
                        conditionB.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.print("B");
                state++;
                conditionC.signal();
                lock.unlock();
            }
        });

        Thread threadC=new Thread(()->{
            for (int i = 0; i < 10; i++) {
                lock.lock();
                while (state%3!=2){
                    try {
                        conditionC.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                System.out.println("C");
                state++;
                conditionA.signal();
                lock.unlock();
            }
        });
        threadA.start();
        threadB.start();
        threadC.start();
    }
}